﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using RanchBuddy.Core.Domain;
using RanchBuddy.Core.Services;
using StructureMap;

namespace RanchBuddy.Core.Attributes
{
    public class MustBeLoggedInAttribute : ActionFilterAttribute 
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (ObjectFactory.GetInstance<IWebContext>().Account == null)
            {
                string loginUrl = "~/Account/LogOn";

                filterContext.HttpContext.Response.Redirect(loginUrl);
            }

            base.OnActionExecuting(filterContext);
        }

        public static bool IsUserLoggedIn()
        {
            if (ObjectFactory.GetInstance<IWebContext>().Account == null)
            {
                return false;
            }
            return true;
        }
    }
}